package chapter02_linkedList;


/**
 * 描述：
 * 打印两个链表的公共部分
 *
 * @author hl
 * @date 2021/1/22 10:07
 */
public class PrintCommonPart {
    public void printCommonPart(Node head1, Node head2) {
        if (head1 == null || head2 == null) {
            return;
        }
        Node h1 = head1, h2 = head2;
        System.out.println("Common part:");
        while (h1 != null && h2 != null) {
            if (h1.val < h2.val) {
                h1 = h1.next;
            } else if (h1.val > h2.val) {
                h2 = h2.next;
            } else {
                System.out.print(h1.val + " ");
                h1 = h1.next;
                h2 = h2.next;
            }
        }
        System.out.println();
    }
}
